package com.caishi.lkx.exam.api;


import com.caishi.lkx.common.BaseApi;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.exam.model.EveryDayPracticeModel;
import com.caishi.lkx.exam.records.RecordsCollectType;
import com.caishi.lkx.exam.records.RecordsDelService;
import com.caishi.lkx.exam.records.RecordsGetDataService;
import com.caishi.lkx.exam.service.IEveryDayPracticeService;
import com.caishi.lkx.exam.vo.EveryDaySectionListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Tag(name = "每日一练相关接口")
@Slf4j
@RestController
@RequestMapping("/exam/every-day-practice")
public class EveryDayPracticeApi extends BaseApi {

    @Resource
    private IEveryDayPracticeService service;

    @Resource
    private RecordsGetDataService recordsGetDataService;
    @Resource
    private RecordsDelService recordsDelService;

    @GetMapping("/detail/{id}")
    @Operation(summary = "每日一练详情")
    public EveryDayPracticeModel detail(@RequestParam String industryCode,
                                        @RequestParam String examId,
                                        @RequestParam Long categoryId,@PathVariable String id) {
        EveryDayPracticeModel model =  service.getById(id);
        model.setRecords( recordsGetDataService.getQuestionRecordsResult(userContext.currentUserId(), industryCode, examId, categoryId, RecordsCollectType.everyday, model.getId()));

        return model;
    }

    @GetMapping("/delete/{id}")
    @Operation(summary = "每日一练删除")
    public Boolean delete(@PathVariable String id) {
        return service.delete(id);
    }

    @GetMapping("/today-practice")
    @Operation(summary = "今日每日一练")
    @Roles
    public EveryDayPracticeModel todayPractice(@RequestParam Long categoryId) {
        return setJoinCount(service.todayPractice(userContext.loginStatus() ? userContext.currentUserId() : userContext.getDuid(), categoryId,LocalDate.now()));
    }

    @GetMapping("/aday-practice")
    @Operation(summary = "获取某一天的每日一练信息")
    @Roles
    public EveryDayPracticeModel adayDetail(@RequestParam Long categoryId, @RequestParam LocalDate date) {
        EveryDayPracticeModel everyDayPracticeModel = service.adayDetail(userContext.loginStatus() ? userContext.currentUserId() : userContext.getDuid(), categoryId, date);
        if (null == everyDayPracticeModel) {
            everyDayPracticeModel = service.todayPractice(userContext.loginStatus() ? userContext.currentUserId() : userContext.getDuid(), categoryId,date);
        }
        return setJoinCount(everyDayPracticeModel);
    }

    @GetMapping("/section-practice")
    @Operation(summary = "获取某个时间段的每日一练")
    @Roles
    public List<EveryDaySectionListVo> sectionList(@RequestParam Long categoryId, @RequestParam(required = false) LocalDate start, @RequestParam(required = false) LocalDate end) {
        return service.sectionList(userContext.loginStatus() ? userContext.currentUserId() : userContext.getDuid(), categoryId, start, end).parallelStream().peek(v -> v.setData(setJoinCount(v.getData()))).toList();
    }

    @GetMapping("/continuity-days")
    @Operation(summary = "获取用户连续学习天数")
    @Roles
    public Integer fetchContinuity() {
        return service.getContinuity(userContext.loginStatus() ? userContext.currentUserId() : userContext.getDuid());
    }

    private EveryDayPracticeModel setJoinCount(EveryDayPracticeModel model) {
        if (model == null) return null;
        model.setJoinCount(service.getJoinCount(model.getCategoryId(), model.getDay()));
        return model;
    }
}
